#	m42-xmas-memory
#   Copyright (C) 2014  Jannik Haberbosch
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame
pygame.init()

from code.constants import colours

from code.classes.Text import Text


class Button( pygame.sprite.Sprite ):
	global_clicked = False
	
	def __init__( self, font, text, pos_center, return_code, colour_inactive = colours.WHITE,
		colour_active = colours.YELLOW ):
		pygame.sprite.Sprite.__init__( self )

		self.button_inactive = Text( font, text, pos_center, colour_inactive )
		self.button_active = Text( font, text, pos_center, colour_active )

		self.states = { "inactive": True, "active": False }
		self.change_state()

		self.return_code = return_code


	def change_state( self ):
	
		if self.states["inactive"]:
			self.image = self.button_inactive.image
			self.rect = self.button_inactive.rect
		elif self.states["active"]:
			self.image = self.button_active.image
			self.rect = self.button_active.rect


	def check_mouse_pos( self ):
		if self.rect.collidepoint( pygame.mouse.get_pos()):
			self.states["active"] = True
			self.states["inactive"] = False
		else:
			self.states["active"] = False
			self.states["inactive"] = True

		self.change_state()


	def check_if_mouse_left_click( self ):
		if pygame.mouse.get_pressed()[0] and not Button.global_clicked:
			Button.global_clicked = True
			return True
		if Button.global_clicked:
			if not pygame.mouse.get_pressed()[0]:
				Button.global_clicked = False
				return False
		return False


	def update( self ):
		self.check_mouse_pos()
		if self.states["active"]:
			return self.check_if_mouse_left_click()
		else:
			return False
